const {
    add,
    del,
    edit,
    search,
    searchBy,
} = require('../js/mysql.js')
const router = require('koa-router')()
const { dateFormat } = require('@jiess/utils')
const jwtAuth = require('../jwtAuth')

router.use(jwtAuth)

router.prefix('/fronttrain')

// 获取培训列表
router.get('/get', async (ctx, next) => {
    const { page = 1, limit = 10, search = "" ,train_admin} = ctx.request.query;
    let sql1 = `user_state=1 and train_admin=${train_admin} and train_state!=6 and (train_name LIKE '%${search}%' or train_admin LIKE '%${search}%')`;
    let sql2 = `user_state=1 and train_admin=${train_admin} and train_state!=6 and (train_name LIKE '%${search}%' or train_admin LIKE '%${search}%') order by id DESC LIMIT ${(page - 1) * limit},${limit}`;
    let total = await searchBy(sql1, 'train');
    let data = await searchBy(sql2, 'train');
    let format = 'yyyy-MM-dd HH:mm:ss';
    ctx.body = {
        code: 200,
        msg: "操作成功",
        data: data.map(item => {
            item.begin = dateFormat(item.begin, format)
            item.start_time = dateFormat(item.start_time, format)
            item.end_time = dateFormat(item.end_time, format)
            return item;
        }),
        total: total.length
    }
})
module.exports = router
